package org.kia.data.model;

import java.io.IOException;

import android.os.Parcel;

import com.github.droidfu.cachefu.CachedModel;
import com.github.droidfu.cachefu.ModelCache;
import com.google.gson.annotations.SerializedName;

/**
 * 
 * @author Amir Lazarovich
 */
public class CategoryModel extends CachedModel {

	//////////////////////////////////////////////
	// Members
	//////////////////////////////////////////////
	
	protected String mCachedId;
	
	@SerializedName("name")
	protected String mName;
	
	@SerializedName("url")
	protected String mImage;

	//////////////////////////////////////////////
	// Constructors
	//////////////////////////////////////////////
	
	/**
	 * Constructs a new Category Model
	 */
	public CategoryModel() {
		
	}
	
	/**
	 * Constructs a new Category Model with given fields
	 * 
	 * @param id
	 * @param name
	 * @param image
	 */
	public CategoryModel(String id, String name, String image) {
		super();
		setModelId(id);
		this.mName = name;
		this.mImage = image;
	}
	
	/**
     * Constructor setting variables from parcel. Same as using a blank constructor and calling readFromParcel.
     *
     * @param source Parcel to be read from.
     * @throws java.io.IOException
     */
    public CategoryModel(Parcel source) throws IOException {
        super(source);
    }
	
	//////////////////////////////////////////////
	// Getters & Setters
	//////////////////////////////////////////////

    public String getId() {
    	return mCachedId;
    }
    
    public void setId(String id) {
    	mCachedId = id;
    }
    
    public void setModelId(String id) {
    	super.setId(id);
    }
    
    public String getModelId() {
    	return super.getId();
    }
    
	public String getName() {
		return mName;
	}

	public void setName(String name) {
		this.mName = name;
	}

	public String getImage() {
		return String.format("http://ladaatmerosh.appspot.com%s", mImage);
	}

	public void setImage(String image) {
		this.mImage = image;
	}
	
	//////////////////////////////////////////////
	// Parcel
	//////////////////////////////////////////////

    /**
     * Creator object used for parceling
     */
    public static final Creator<CategoryModel> CREATOR = new Creator<CategoryModel>() {

        public CategoryModel createFromParcel(Parcel source) {
            try {
                return new CategoryModel(source);
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }

        public CategoryModel[] newArray(int size) {
            return new CategoryModel[size];
        }
    };
	
	//////////////////////////////////////////////
	// Overrides
	//////////////////////////////////////////////

    @Override
    public boolean reloadFromCachedModel(ModelCache modelCache, CachedModel cachedModel) {
        CategoryModel cachedObject = (CategoryModel) cachedModel;
        mCachedId = cachedObject.mCachedId;
        mName = cachedObject.mName;
        mImage = cachedObject.mImage;
        return false;
    }

    @Override
    public void readFromParcel(Parcel source) throws IOException {
        super.readFromParcel(source);
        mCachedId = source.readString();
        mName = source.readString();
        mImage = source.readString();
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        super.writeToParcel(dest, flags);
        dest.writeString(mCachedId);
        dest.writeString(mName);
        dest.writeString(mImage);
    }
    
    @Override
    public String getKey() {
        if (mCachedId == null) {
            return null;
        } else {
            return createKey(mCachedId);
        }
    }

    @Override
    public String createKey(String id) {
        return id;
    }
}
